// counterStore.js
import { shallowReactive,markRaw,toRaw } from 'vue'
import { defineStore } from 'pinia'
import { PlusIcon, AlertCircleIcon, ChevronDown, Minus, Braces,MinusCircleIcon } from 'lucide-vue-next'
import { Textarea } from '@/components/ui/textarea'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import { Switch } from '@/components/ui/switch'
import { Button } from '@/components/ui/button'
// import Holder from '@/components/vue-flow/nodes/holder.vue'
// import SelectComponent from '@/components/vue-flow/nodes/selectComponent.vue'
import {Option} from '@/components/vue-flow/nodes/types/form-types'

const template = {
  id:'module_1',
  type:'module',
  "data": {
			"title": "模型节点",
      "properties":{
        'json': {
          "icon": 'Braces',
          "size": 16,
          "color": 'rgb(77, 83, 232)',
        },
      },
    'labels': [
      {"type":'Label', "label": '参数名', "key": 'name','class':'block text-xs text-gray-400 w-150'},
      {"type":'Label', "label": '变量值', "key": 'type' ,'class':'block text-xs text-gray-400 w-[130px]'},
      {"type":'Label', "label": '描述', "key": 'description' ,'class':'block text-xs text-gray-400 w-[150px]'}
    ],
			"llm": {
				"name": "GLM(glm-4-0520)",
				"randomness": 0.5,
				"topP": 0.3,
				"maxResponseLength": 1024
			},
      'description':'请提供需要添加到提示词中的信息，这些信息可以被提示词引用。',
      'placeholder':'用户提示词，可以使用{{变量名}}、{{变量名.子变量名}}、{{变量名[数组索引]}}的方式引用输入参数中的变量',
			"prompt": "",
			"input": [{
				"ref": "",
				"name": "",
				"category": "1",
				"value": "",
			}],
			"output": [{
				"name": "output",
				"type": "1",
        "description":'',
       
			}]
		}
}
export const useLLMStore = defineStore({
  id: 'useLLMStore',
  state: () => ({
    template
  }),
  actions: {
    init(initData:any){
      if(initData){
        this.template.data.prompt = initData.data.prompt
        this.template.data.input =initData.data.input.map(item=>({
          "ref": item.ref,
          "name": item.name,
          "category": item.category,
          "value": item.value,
        }))
        this.template.data.output = initData.data.output.map(item=>({
          "name": item.name,
          "type": item.type,
          "description": item.description,
          "action": 0
        }))
        this.template = {...this.template}
      }
    },
    setId(id:string){
      this.template.id = id;
    },
    add() {
      this.template.data.input.push({
				"ref": "",
				"name": "",
				"category": "1",
				"value": "",
			})
    },
    outputadd(){
      this.template.data.output.push({
				"name": "",
				"type": "1",
        "description":'',
        "action": this.template.data.output.length
			})
    },
    deleteItem(index) {
      this.template.data.input.splice(index, 1)
    },
    deleteOutputItem(index) {
      this.template.data.output.splice(index, 1)
    },
    setInputOptions(data:any,ref:string){
      const defaultValue = data.value[0]?.name;
      this.template.data.input.forEach(item=>{
        item.value = defaultValue
        item.ref = ref
      })
    },
    updateInputValue(index, key, value){
      this.template.data.input[index][key] = value
    },
    updateRef(id:string){
      this.template.data.input.forEach(element => {
        element.ref = id;
      });
    },
    updateValue(index, key, value){
      this.template.data.output[index][key] = value
    },
    clearData(){
      this.template.data.input = this.template.data.input.map(item=>{
        item.value = ''
        return item
      })
    }
  }
})
